package com.lagou.hadoop.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author chenkl
 * @Date 2020/8/9 17:31
 */
public class SortBean implements WritableComparable<SortBean> {
    /**
     * 排名
     */
    private int rank;
    /**
     * 值
     */
    private int value;

    public SortBean() {
    }

    public SortBean(int rank, int value) {
        this.rank = rank;
        this.value = value;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    /**
     * 指定排序字段，此次对value进行升序
     * @param o
     * @return
     */
    @Override
    public int compareTo(SortBean o) {
        if(this.value>o.getValue()){
            return 1;
        }else if(this.value<o.getValue()){
            return -1;
        }else {
            return 0;
        }
    }

    /**
     * 序列化
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(rank);
        out.writeInt(value);
    }

    /**
     * 反序列化
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.rank = in.readInt();
        this.value = in.readInt();
    }

    @Override
    public String toString() {
        return  rank +
                "\t" + value;
    }
}
